package com.manage.auth;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.common.util.DateUtils;
import com.common.util.Tools;
import com.model.auth.Tie;
import com.model.auth.User;

import com.opensymphony.xwork2.ActionSupport;

import com.service.auth.ITieService;


public class TieAction extends ActionSupport {

	/**
	 * 
	 */
	private static final Logger log = Logger.getLogger(TieAction.class);
	private static final long serialVersionUID = 1L;
	private static final int BUFFER_SIZE = 16 * 1024;  
	@Resource
	private ITieService tieService;
	
	private Tie tie;
	private Tie tie1;
	private Tie tie2;	
	private Tie tie3;
	private Tie tie4;
	
    private User user;
	private Integer type;
    
    private Integer id;
	
	private File myFile;
	   
    private String myFileFileName;   // 上传文件名  
    
    private String myFileContentType; // 上传文件类型   

    private List<Tie> tieList;
    private List<Tie> leaveList;



private String result;
    
    public String web(){
    	try {
    		tie1=tieService.getTieById(1);
    		tie2=tieService.getTieById(2);
    		tie3=tieService.getTieById(3);
    		tie4=tieService.getTieById(4);
    		tieList =  tieService.getTieInfo(10);
    		leaveList=tieService.getTieInfo(11);
    		user=Tools.getCurrentUser();
    		
		} catch (Exception e) {
			log.error("读取信息列表异常", e);
		}
    	return "web";
    }
    
    public Tie getTie4() {
		return tie4;
	}

	public void setTie4(Tie tie4) {
		this.tie4 = tie4;
	}

	public String addWeb(){
    	try {
			if(tie!=null){
				tie.setDatetime(DateUtils.getDateMinuteFormat());
				tieService.insertTie(tie);	
			}
			result ="1";
		} catch (Exception e) {
			
			result = "0";
			log.error("读取信息列表异常", e);
		}
    	return SUCCESS;
    }

    
    public String list(){
    	try {
    		tieList =  tieService.getTieInfo(type);
		} catch (Exception e) {
			log.error("读取信息列表异常", e);
		}
		return "list";  	
    }
    
    public String messagelist(){
    	try {
    		tieList =  tieService.getTieInfo(type);
		} catch (Exception e) {
			log.error("读取信息列表异常", e);
		}
		return "messagelist";  	
    }
    
	public String save(){
		try{
			if(tie!=null){
				if(myFile!=null){
					String dateFolder = DateUtils.toString(new Date(), "yyyy-MM-dd").replace("-", "/"); //用年月日作为上传图片的存放路径
					String fileDate=DateUtils.toString(new Date(), "yyyyMMddhhmmss"); //上传的名称用年月日时分秒做为文件名称
					String fileName=myFileFileName; //myFileFileName传来的文件名称
					String path=ServletActionContext.getServletContext().getRealPath("UserFiles") + "/" + dateFolder;//存放上传文件的绝对路径
					fileName = fileDate + fileName.substring(fileName.lastIndexOf("."),fileName.length());
			        File dstFile = new File(new File(path),fileName);
			    	if (!dstFile.getParentFile().exists()) {
			    		dstFile.getParentFile().mkdirs();
					}
			    	String images=dateFolder+"/"+fileName;
			    	copy(myFile,dstFile); 
			    	dstFile.createNewFile();
			    	tie.setImages(images);
			    	tie.setDatetime(DateUtils.getDateMinuteFormat());
					tieService.insertTie(tie);
				}else{
					tie.setDatetime(DateUtils.getDateMinuteFormat());
					tieService.insertTie(tie);						
					}
			}
		}catch(Exception e){
			log.error("添加信息！", e);
		}
		return "save";	
	}
	
	
	public String update(){
		try{
			if(tie!=null){
				if(myFile!=null){
					String dateFolder = DateUtils.toString(new Date(), "yyyy-MM-dd").replace("-", "/"); //用年月日作为上传图片的存放路径
					String fileDate=DateUtils.toString(new Date(), "yyyyMMddhhmmss"); //上传的名称用年月日时分秒做为文件名称
					String fileName=myFileFileName; //myFileFileName传来的文件名称
					String path=ServletActionContext.getServletContext().getRealPath("UserFiles") + "/" + dateFolder;//存放上传文件的绝对路径
					fileName = fileDate + fileName.substring(fileName.lastIndexOf("."),fileName.length());
			        File dstFile = new File(new File(path),fileName); 
			    	if (!dstFile.getParentFile().exists()) {
			    		dstFile.getParentFile().mkdirs();
					}
			    	String images=dateFolder+"/"+fileName;
			    	copy(myFile,dstFile);
			    	dstFile.createNewFile(); 
			    	 File delOlderFile= new File(ServletActionContext.getServletContext().getRealPath( "UserFiles" )+"/"+tie.getImages());
						if(delOlderFile.exists()){
							delOlderFile.delete();
						}
					tie.setImages(images);
					tie.setDatetime(DateUtils.getDateMinuteFormat());
					tieService.insertTie(tie);	
				}else{
					Tie temp = tieService.getTieById(tie.getId());
					tie.setImages(temp.getImages());
					tie.setDatetime(DateUtils.getDateMinuteFormat());
					tieService.insertTie(tie);	
					}
			}
		}catch(Exception e){
			log.error("添加信息！", e);
		}
		return "update";	
	}

	
	public String delete(){
		
		try{
			if(id!=null){
				Tie temp = tieService.getTieById(id);
				if(temp.getImages()!=null){
		    	File delOlderFile= new File(ServletActionContext.getServletContext().getRealPath( "UserFiles" )+"/"+temp.getImages());
					if(delOlderFile.exists()){
						delOlderFile.delete();	
					}
					tieService.delTieById(id);
				}else{
					tieService.delTieById(id);
				}    	
		    }
		}catch(Exception e){
			log.error("删除模块列表异常", e);
		}
		return SUCCESS;
	}
	
	public String edit(){
		try{
			if(id!=null){
				tie = tieService.getTieById(id);
			}
		}catch(Exception e){
			log.error("查询新闻详细", e);
		}
		return "edit";
	}
	
	
	public String type(){
		try{
			if(id!=null){
				tie = tieService.getTieById(id);
			}
		}catch(Exception e){
			log.error("查询详细信息", e);
		}
		return "type";
	}
	
	public String typeUpdate(){
		try{
			if(tie!=null){
				tieService.updateTie(tie);
			}
		}catch(Exception e){
			log.error("查询详细信息", e);
		}
		return "typeUpdate";
	}
	

	/**
	 * 处理上传的信息处理
	 * @author 蒋文书
	 * **/
	private static void copy(File src, File dst) throws Exception {
		   try {
				InputStream in = null;
				OutputStream out = null;
				try {
					in = new BufferedInputStream(new FileInputStream(src),
							BUFFER_SIZE);
					out = new BufferedOutputStream(new FileOutputStream(dst),
							BUFFER_SIZE);
					byte[] buffer = new byte[BUFFER_SIZE];
					while (in.read(buffer) > 0) {
						out.write(buffer);
					}
				}catch(Exception e1){
					e1.printStackTrace();
					//throw new Exception("图片存储出错");
				} finally {
					if (null != in) {
						in.close();
					}
					if (null != out) {
						out.close();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				//throw new Exception("图片存储出错");
			}
		} 
	
	   public List<Tie> getLeaveList() {
			return leaveList;
		}

		public void setLeaveList(List<Tie> leaveList) {
			this.leaveList = leaveList;
		}
    public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public Tie getTie3() {
		return tie3;
	}

	public void setTie3(Tie tie3) {
		this.tie3 = tie3;
	}

	public Tie getTie1() {
		return tie1;
	}


	public void setTie1(Tie tie1) {
		this.tie1 = tie1;
	}


	public Tie getTie2() {
		return tie2;
	}


	public void setTie2(Tie tie2) {
		this.tie2 = tie2;
	}

	
	public List<Tie> getTieList() {
		return tieList;
	}

	public void setTieList(List<Tie> tieList) {
		this.tieList = tieList;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Tie getTie() {
		return tie;
	}


	public void setTie(Tie tie) {
		this.tie = tie;
	}


	public Integer getType() {
		return type;
	}


	public void setType(Integer type) {
		this.type = type;
	}

}
